<template>
    <div class="video-talk-list" v-loading="loading">
        <div class="input-box">
            <el-input
                placeholder="请输入您要搜索的内容"
                clearable
                @keyup.enter.native="getList(1)"
                style="width:200px;"
                v-model.trim="keyword">
                <i slot="prefix" class="el-input__icon el-icon-search"></i>
            </el-input>
            <el-select
                style="margin-left:10px;width:300px;"
                v-model="gameId"
                clearable
                filterable
                @change="getList(1)"
                :filter-method="filterMethod"
                :loading="gameLoading"
                placeholder="请输入并选择游戏">
                <el-option
                    v-for="item in gameList"
                    :key="item.gameid"
                    :label="item.appName"
                    :value="item.gameid">
                    <span style="float: left">{{ item.appName }}</span>
                    <span style="float: right; color: #8492a6; font-size: 13px">{{ item.gameid }}</span>
                </el-option>
            </el-select>
            <el-button type="primary" class="btn" @click="getList(1)">搜索</el-button>
        </div>
        <div class="table-box">
            <el-table
                :data="tableData"
                border
                style="width: 100%">
                <el-table-column
                    fixed
                    prop="commentid"
                    label="点评id">
                </el-table-column>
                <el-table-column
                    width="180"
                    prop="content"
                    label="点评内容">
                </el-table-column>
                <el-table-column
                    prop="videoid"
                    label="videoid">
                    <template slot-scope="scope">
                        <el-button @click="goVideoDetail(scope.row.videoid)" type="text" size="small">{{scope.row.videoid}}</el-button>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="gamename"
                    label="关联游戏">
                </el-table-column>
                <el-table-column
                    prop="thumbsNum"
                    sortable
                    label="点赞数">
                </el-table-column>
                <!-- <el-table-column
                    prop="gameTopic"
                    label="举报数">
                </el-table-column> -->
                <el-table-column
                    prop="replyNum"
                    sortable
                    label="回复数">
                </el-table-column>
                <el-table-column
                    prop="username"
                    label="点评者">
                </el-table-column>
                <el-table-column
                    prop="createTime"
                    label="点评时间">
                </el-table-column>
                <el-table-column
                    fixed="right"
                    label="点评操作">
                    <template slot-scope="scope">
                        <!-- -2系统删除-1用户删除0已禁用1显示中 -->
                        <div v-if="scope.row.status === 1"><el-button @click="changeVideoTalkStatus(scope.row, 0)" type="text" size="small" style="color:#F56C6C;">禁用</el-button></div>
                        <div v-else-if="scope.row.status === 0"><el-button @click="changeVideoTalkStatus(scope.row, 1)" type="text" size="small" style="color:#67C23A;">启用</el-button></div>
                        <div><el-button @click="goCommentDetail(scope.row.commentid)" type="text" size="small">查看详情</el-button></div>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
export default {
    data () {
        return {
            keyword: '',
            gameId: '',
            gameList: [],
            tableData: [],
            pageNum: 1,
            pageSize: this._pageSize,
            total: 0,
            gameLoading: false,
            loading: false
        }
    },
    created () {
        // 更好的缓存数据
        if (this.listParams) {
            this.keyword = this.listParams.keyword
            this.gameId = this.listParams.gameId
            this.gameList = this.listParams.gameList
        }
        this.getList(this.pageNum)
    },
    methods: {
        // 获取点评列表
        async getList (pageNum) {
            if (this.loading) return
            this.loading = true
            this.pageNum = pageNum
            this.listParams = {
                pageIndex: this.pageNum,
                pageNum: this.pageSize
            }
            if (this.keyword) {
                this.listParams.keyword = this.keyword
            }
            if (this.gameId) {
                this.listParams.gameId = this.gameId
            }
            let res = await this.$http.post('/video/findVideoComment', this.listParams)
            if (res.code === '0') {
                this.tableData = res.data
                this.total = res.count
                this.listParams.gameList = this.gameList
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.loading = false
        },

        // 查询游戏
        filterMethod (res) {
            clearTimeout(this.timerId)
            if (!res) {
                this.gameLoading = false
                return
            }
            this.gameLoading = true
            this.timerId = setTimeout(() => {
                this.getgameList(res)
            }, 1000)
        },
        async getgameList (keyword) {
            let params = {
                keyword,
                pageIndex: 1,
                pageNum: 100
            }
            let res = await this.$http.post('/home/findGames', params)
            if (res.code === '0') {
                this.gameList = res.data
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.gameLoading = false
        },

        // 页容切换
        sizeChange (num) {
            this.pageSize = num
            this.getList(1)
        },

        // 页码切换
        pageChange (num) {
            this.pageNum = num
            this.getList(this.pageNum)
        },

        // 跳转至视频详情
        goVideoDetail (id) {
            this.$router.push({
                path: '/video/videoDetail',
                query: {
                    id
                }
            })
        },

        // 更改视频状态
        async changeVideoTalkStatus (item, status) {
            // 状态:0禁用 1启用
            let params = {
                commentId: item.commentid,
                status
            }
            let res = await this.$http.post('/video/updateVideoCommentStatus', params)
            if (res.code === '0') {
                item.status = status
                this.$message({
                    type: 'success',
                    message: '修改成功'
                })
            } else {
                this.$message({
                    type: 'error',
                    message: ''
                })
            }
        },

        // 查看视频详情
        goCommentDetail (id) {
            if (id) {
                this.$router.push({
                    path: '/video/videoTalkDetail',
                    query: {
                        id
                    }
                })
            } else {
                this.$router.push('/video/videoTalkDetail')
            }
        }
    }
}
</script>

<style lang="scss" scoped>
    .video-talk-list {
        .input-box {
            .btn {
                margin-left: 10px;
            }
        }
        .table-box {
            margin: 20px 0;
            .app-icon {
                width: 60px;
                height: 60px;
                > img {
                    width: 100%;
                    height: 100%;
                }
            }
        }
        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
</style>
